package com.dinim.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Random;

import com.dinim.matrix.twodimensional.IPoint2D;

public class GraphicsContextSwingImpl implements IGraphicsContext{

	private Graphics g;
	
	public void setG(Graphics g) {
        this.g = g;
    }

    public GraphicsContextSwingImpl(Graphics g){
		this.g = g;
	}

	public void drawLine(int x1, int y1, int x2, int y2) {
		g.drawLine(x1, y1, x2, y2);
	}

    public void drawLine(double x1, double y1, double x2, double y2) {
        g.drawLine((int)Math.round(x1), (int)Math.round(y1), 
                   (int)Math.round(x2), (int)Math.round(y2));
    }

    public void drawLine(IPoint2D p1, IPoint2D p2) {
        g.drawLine((int)Math.round(p1.getX()), (int)Math.round(p1.getY()), 
                   (int)Math.round(p2.getX()), (int)Math.round(p2.getY()));
    }
    
	public void setAntialiasing(boolean antialiasing) {
		if(antialiasing){
			Graphics2D g2 = (Graphics2D)g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
							    RenderingHints.VALUE_ANTIALIAS_ON);
		} else {
			Graphics2D g2 = (Graphics2D)g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
							    RenderingHints.VALUE_ANTIALIAS_OFF);
		}
	}

	public void drawOval(int x1, int y1, int x2, int y2) {
        if(x1>x2){
            int xbuf = x1;
            x1 = x2;
            x2 = xbuf;
        }
        if(y1>y2){
            int ybuf = y1;
            y1 = y2;
            y2 = ybuf;
        }
		g.drawOval(x1, y1, x2-x1, y2-y1);
	}

	public void drawRectangle(int x1, int y1, int x2, int y2){
        if(x1>x2){
            int xbuf = x1;
            x1 = x2;
            x2 = xbuf;
        }
        if(y1>y2){
            int ybuf = y1;
            y1 = y2;
            y2 = ybuf;
        }
        g.drawRect(x1, y1, x2-x1, y2-y1);
    }

    public void drawRectangle(double x1, double y1, double x2, double y2) {
        if(x1>x2){
            double xbuf = x1;
            x1 = x2;
            x2 = xbuf;
        }
        if(y1>y2){
            double ybuf = y1;
            y1 = y2;
            y2 = ybuf;
        }
        g.drawRect((int)Math.round(x1), (int)Math.round(y1), 
                   (int)Math.round(x2-x1), (int)Math.round(y2-y1));
    }

    public void drawFilledRectangle(double x1, double y1, double x2, double y2) {
        if(x1>x2){
            double xbuf = x1;
            x1 = x2;
            x2 = xbuf;
        }
        if(y1>y2){
            double ybuf = y1;
            y1 = y2;
            y2 = ybuf;
        }
        //g.setColor(Color.white);
        g.fillRect((int)Math.round(x1), (int)Math.round(y1), 
                   (int)Math.round(x2-x1), (int)Math.round(y2-y1));
        //g.setColor(Color.black);
    }

    public void drawText(String text, int x, int y) {
        g.drawString(text, x, y);
    }

	public void setColor(Color color) {
		g.setColor(color);
	}
    
}
